#include "StdAfx.h"
#include "VlareImpl.h"
#include "OpenGLVisualizer.h"

void VlareImpl::Init(VlareHashMap properties)
{
	VlareHashMap hm;
	hm.insert(VlarePair(VEM_POSITIONX, DynamicData(0.0)));
	hm.insert(VlarePair(VEM_POSITIONY, DynamicData(0.0)));
	hm.insert(VlarePair(VEM_WIDTH, DynamicData(0.2)));
	hm.insert(VlarePair(VEM_HEIGHT, DynamicData(0.2)));
	//TODO: Pluginchoosing later!
	pRoot = new Element(0x0, hm);
	pViz = new OpenGLVisualizer();
}
void VlareImpl::AcquireResources()
{
}

void VlareImpl::FreeResources()
{
}

void VlareImpl::Free()
{
	// TODO: in Element operator delete auslagern, dann nur delete der wurzel aufrufen
	Element *pPos = pRoot;
	while(pPos)
	{
		delete pPos;
		if(pPos->pChildren)
			pPos = pPos->pChildren;
		else
			pPos = pPos->pNext;
	}
}

void VlareImpl::RunFrame()
{
	this->pViz->PreDraw();
	Element *pPos = this->pRoot;
	while(pPos)
	{
		this->pViz->DrawElement(pPos, 0x0);
		if(pPos->pChildren)
			pPos = pPos->pChildren;
		else
			pPos = pPos->pNext;
	}
	this->pViz->PostDraw();
}

Element* VlareImpl::AddElement(Element *const pParent, ElementMemberMap props)
{
	if(!pParent)
		return (Element*)0x0;
	Element* pPos = pParent->pChildren;
	pParent->pChildren = new Element(pParent);
	pParent->pChildren->pNext = pPos;
	//MARK: pPrev
	//pPos->pPrev = pParent->pChildren;
	return pParent->pChildren;
}
Element *const VlareImpl::GetRoot()
{
	return pRoot;
}
void VlareImpl::BringToFront(const Element *pElem)
{
}

void VlareImpl::DeleteElement(const Element *pElem)
{
}

DynamicData::VLARE_TYPE VlareImpl::IsHook(const char *const szFunction)
{
	return DynamicData::TYPE_NULL;
}

DynamicData VlareImpl::GetHook(Element *pElem, const char *const szName, DynamicData *params)
{
	return DynamicData();
}

void VlareImpl::SetHook(Element *const pElem, const char *const szName, DynamicData val, bool bCacheValueForFrame)
{
	(*pElem)[szName] = val;
}

void VlareImpl::UnsetHook(Element *const pElem, const char *const szName, DynamicData val)
{

}

VlareImpl::VlareImpl(void)
{
}

VlareImpl::~VlareImpl(void)
{
}
